package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.dao.entity.ExtensionDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 第三方扩展表 Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2023-04-10
 */
public interface ExtensionMapper extends BaseMapper<ExtensionDO> {

    String QUERY_SQL_ID = "SELECT a.Extension_id FROM TMS_Extension a left join TMS_OrderIndex b on b.Index_ID = a.Extension_OrderCID";

    String WHERE = " where a.Extension_type = 0";

    String ORDER_BY = " ORDER BY a.Extension_id";

    @Select(QUERY_SQL_ID + WHERE + ORDER_BY + " OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    List<Long> getTopOrderId(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);

    @Select("SELECT count(*) FROM TMS_Extension a left join TMS_OrderIndex b on b.Index_ID = a.Extension_OrderCID " + WHERE)
    Integer count();

    @Select("select a.*,b.Index_PactCode as contractNo,b.Index_CreatorCompanyID as tenantId,b.Index_TransportMode as transportMode,b.Index_Code as orderNo,CASE when b.Index_Status = 32 THEN 1 ELSE 0 end as closed,b.Index_CustomerID as customerId,b.Index_Creator as createMan " +
            ",b.Index_FromMan as contactName, b.Index_FromContact as contactMobile " +
            "from TMS_Extension a left join TMS_OrderIndex b on b.Index_ID = a.Extension_OrderCID where a.Extension_type = 0 and a.Extension_id = #{id}")
    ExtensionDO getById(@Param("id") Long id);

    @Select(QUERY_SQL_ID + WHERE + " and a.Extension_InsertTime >= #{startTime} and a.Extension_InsertTime <= #{endTime} " + ORDER_BY)
    List<Long> getByTime(@Param("startTime") String startTime, @Param("endTime") String endTime);
}
